const app = getApp();
Page({
  data:{
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '查看单据附件', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     navbarHeight: app.globalData.height * 2 + 20
  },
  onLoad(){
    var applyAttachmentList = wx.getStorageSync('applyAttachmentList');
    for(var i=0; i < applyAttachmentList.length; i++){
      var attachment = applyAttachmentList[i];
      var size = attachment.length / (1024 * 1024);
      size = size.toFixed(2);
      attachment.size = size;
      if(attachment.extensionName== 'pdf'){
        attachment.iconPath = "../../../images/workbench/pdf.png";
      } else if(attachment.extensionName== 'doc' || attachment.extensionName== 'docx'){
        attachment.iconPath = "../../../images/workbench/word.png";
      } else if(attachment.extensionName== 'xls' || attachment.extensionName== 'xlsx'){
        attachment.iconPath = "../../../images/workbench/excel.png";
      } else if(attachment.extensionName== 'ppt' || attachment.extensionName== 'pptx'){
        attachment.iconPath = "../../../images/workbench/ppt.png";
      }
    }
    this.setData({
      attachmentList:applyAttachmentList
    });
  },
  onShow(){
  },
  bindFileClick(e){
    var attachment = e.currentTarget.dataset.attachment;
    // wx.setStorageSync('fileUrl', attachment.fileUrl);
    // wx.navigateTo({
    //   url: '../filescan/index',
    // })
    wx.showLoading({
      title: '正在加载文档',
      mask:true
    })
    wx.downloadFile({
      url: attachment.fileUrl,
      success: function (res) {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          fileType:'pdf',
          showMenu:true,
          success: function (res) {
            
          },
          fail:function(f){
            wx.showToast({
              title:'文档打开失败！',
              icon:'error'
            })
          },
          complete:function(c){
            wx.hideLoading({
              success: (res) => {},
            })
          }
        })
      },
      fail:function(f){
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showToast({
          title:'文档下载失败！',
          icon:'error'
        })
      }
    })
  }
})